﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmlEngine.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// Removes INI comments from INI-styled string arrays (such as world files).
        /// </summary>
        /// <param name="input">The INI-styled string array to use.</param>
        /// <returns>The INI-styled string array stripped of comments.</returns>
        public static string[] RemoveComments(this string[] input)
        {
            List<String> lines = new List<string>();
            lines = input.ToList<String>();
            int i = 0;
            while (i < lines.Count)
            {
                // comment at beginning of line
                if (lines[i].StartsWith(";"))
                {
                    lines.RemoveAt(i);
                }
                // comment in middle of line
                else if (lines[i].Contains(";"))
                {
                    int pos = lines[i].IndexOf(";");
                    lines[i] = lines[i].Substring(0, pos);
                    i++;
                }
                else i++;
            }

            //input = lines.ToArray();
            return lines.ToArray();
        }

        /// <summary>
        /// Trims all the strings in an array.
        /// </summary>
        /// <param name="input">The array of strings to trim.</param>
        /// <returns>An array of trimmed strings.</returns>
        public static string[] TrimArray(this string[] input)
        {
            for (int i = 0; i < input.Length; i++)
            {
                input[i] = input[i].Trim();
            }
            return input;
        }
    }
}
